package fr.lip6.move.care;

import fr.lip6.move.care.CareGen;
import fr.lip6.move.care.CompileResult;
import fr.lip6.move.care.SDMakerToCareUtils;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.xtend2.lib.StringConcatenation;
import org.eclipse.xtext.xbase.lib.Exceptions;
import sdmakerz_v1.coresyntax.SDMakerV1RuntimeModule;
import sdmakerz_v1.coresyntax.generator.SDMakerV1IDProvider;
import sdmakerz_v1.coresyntax.generator.interfaces.IContext;
import sdmakerz_v1.coresyntax.generator.interfaces.IDiagramRefData;
import sdmakerz_v1.coresyntax.generator.interfaces.IInputData;
import sdmakerz_v1.coresyntax.sDMakerV1.Diagram;
import sdmakerz_v1.coresyntax.sDMakerV1.DiagramElement;
import sdmakerz_v1.coresyntax.sDMakerV1.DiagramRef;
import sdmakerz_v1.coresyntax.sDMakerV1.Reference;

@SuppressWarnings("all")
public class CareDiagramRefGen {
  private CareGen mainGenerator;
  
  public CareDiagramRefGen(final CareGen mainGenerator) {
    this.mainGenerator = mainGenerator;
  }
  
  public CompileResult compile(final IContext context, final DiagramRef diagramRef) {
    try {
      int _computeID = SDMakerV1IDProvider.computeID();
      String refID = ("bloc_ref_id" + Integer.valueOf(_computeID));
      ArrayList<CompileResult> _arrayList = new ArrayList<CompileResult>();
      List<CompileResult> nestedCompileResult = _arrayList;
      IDiagramRefData oldRefData = context.getDiagramRefData();
      Class<? extends IDiagramRefData> _bindIDiagramRefData = SDMakerV1RuntimeModule.bindIDiagramRefData();
      IDiagramRefData newRefData = _bindIDiagramRefData.newInstance();
      IInputData oldInputData = context.getInputData();
      Class<? extends IInputData> _bindIInputData = SDMakerV1RuntimeModule.bindIInputData();
      IInputData inputData = _bindIInputData.newInstance();
      Diagram d = null;
      Reference _ref = diagramRef.getRef();
      Resource _findDiagramInput = context.findDiagramInput(_ref);
      inputData.setInput(_findDiagramInput);
      context.setInput(inputData);
      Reference _ref_1 = diagramRef.getRef();
      Diagram _findDiagram = context.findDiagram(_ref_1);
      d = _findDiagram;
      IDiagramRefData _diagramRefData = context.getDiagramRefData();
      String _name = d.getName();
      newRefData.init(d, diagramRef, _diagramRefData, _name);
      context.setDiagramRefData(newRefData);
      int i = 0;
      StringConcatenation _builder = new StringConcatenation();
      _builder.append("//STARTING ");
      _builder.append(refID, "");
      _builder.newLineIfNotEmpty();
      _builder.newLine();
      {
        EList<DiagramElement> _diagram_elts = d.getDiagram_elts();
        for(final DiagramElement elt : _diagram_elts) {
          _builder.append("        ");
          CompileResult compRes = this.mainGenerator.compile(context, elt);
          _builder.newLineIfNotEmpty();
          {
            Boolean _isAddableToEntryPoint = compRes.getIsAddableToEntryPoint();
            if ((_isAddableToEntryPoint).booleanValue()) {
              _builder.append("        ");
              boolean _add = nestedCompileResult.add(compRes);
              CharSequence _nullExp = SDMakerToCareUtils.toNullExp(Boolean.valueOf(_add));
              _builder.append(_nullExp, "        ");
              _builder.newLineIfNotEmpty();
            }
          }
          _builder.append("        ");
          CharSequence _charSequenceResult = compRes.getCharSequenceResult();
          _builder.append(_charSequenceResult, "        ");
          _builder.newLineIfNotEmpty();
        }
      }
      _builder.append("        ");
      _builder.newLine();
      _builder.append("        ");
      _builder.append("//FINALIZING ");
      _builder.append(refID, "        ");
      _builder.newLineIfNotEmpty();
      {
        int _size = nestedCompileResult.size();
        boolean _greaterThan = (_size > 1);
        if (_greaterThan) {
          _builder.append("        ");
          _builder.append("BlockList ");
          _builder.append(refID, "        ");
          _builder.append(" = new BlockList();");
          _builder.newLineIfNotEmpty();
          {
            for(final CompileResult compRes_1 : nestedCompileResult) {
              _builder.append("        ");
              _builder.append(refID, "        ");
              _builder.append(".add(");
              String _eltID = compRes_1.getEltID();
              _builder.append(_eltID, "        ");
              _builder.append(");");
              _builder.newLineIfNotEmpty();
            }
          }
        } else {
          int _size_1 = nestedCompileResult.size();
          boolean _equals = (_size_1 == 1);
          if (_equals) {
            _builder.append("        ");
            _builder.append("Block ");
            _builder.append(refID, "        ");
            _builder.append("= ");
            CompileResult _get = nestedCompileResult.get(0);
            String _eltID_1 = _get.getEltID();
            _builder.append(_eltID_1, "        ");
            _builder.append(";");
            _builder.newLineIfNotEmpty();
          }
        }
      }
      CharSequence charSequenceResult = _builder;
      context.setDiagramRefData(oldRefData);
      context.setInput(oldInputData);
      CompileResult _compileResult = new CompileResult(charSequenceResult, refID, 
        true);
      return _compileResult;
    } catch (Exception _e) {
      throw Exceptions.sneakyThrow(_e);
    }
  }
}
